//
//  QuoteView.m
//  FreeTime
//
//  Created by ti on 16/9/5.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "QuoteView.h"
#import "Header.h"
@interface QuoteView()<UITextFieldDelegate>{
    UIButton *imgBt;
    UITextField *contentTF;
}
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UIView *hudView, *headView;
@property (nonatomic, strong)UIImageView *imgIV;
@property (nonatomic, strong)NSMutableArray *btnArray;
@property (nonatomic, strong)NSMutableArray *imgArray;
@property(nonatomic,strong)UIWindow *overlayWindow;
@property (nonatomic, strong)UIImageView *imgV;
@property (nonatomic, strong)UIImageView *imgV2;
@end

@implementation QuoteView
//+(QuoteView*)sharedView{
//    
//    static QuoteView *sharedView = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        sharedView = [[QuoteView alloc] init];
//    });
//    return sharedView;
//}

- (instancetype)init {
    self = [super init];
    self.btnArray = [NSMutableArray array];
    self.imgArray = [NSMutableArray array];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 1.0f;
    }
    return self;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];//[UIColor clearColor];
//        _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}
- (void)show {
    [self addSubview:self.bgView];
    [self.overlayWindow addSubview:self];
    [self.overlayWindow makeKeyAndVisible];
    self.bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.bgView addGestureRecognizer:tapGesture];
    
//    [UIView animateWithDuration:0.5 animations:^{
//        self.hudView.frame=CGRectMake((WindowWidth-240)/2,64.5+80,240, 240);
        [self.hudView addSubview:self.headView];
//    }];
}
-(void)dismiss
{
    [UIView animateWithDuration:0.5 animations:^{
        
//        [self.bgView removeFromSuperview];
        [self.hudView removeFromSuperview];
        
        CGRect frame = self.hudView.frame;
        frame.size.height = 0;
        self.hudView.frame = frame;
        
        
    }completion:^(BOOL finished) {
        [self.bgView removeFromSuperview];
        self.overlayWindow = nil;
        self.hudView = nil;
    }];
}
-(UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:self.hudView.bounds];
        NSArray *titleArr = @[@"线上",@"线下"];
        NSArray *imgArr = @[@"skillNor",@"skillLi"];
        CGFloat w = [ToolUtil autoWidth:@"线上" width:0 height:0 num:14];
        for (int i=0; i<2; i++) {
            UILabel *titleL = [ReduceCode creatLabelWithFrame:CGRectMake(54+i*(w+5+10+61), 23, w, 14) title:titleArr[i] titleColor:[ImageUtil colorWithRgb:0xFFFFFF] font:14 textAlignment:NSTextAlignmentLeft];
            [_headView addSubview:titleL];
            
            imgBt = [UIButton buttonWithType:UIButtonTypeCustom];
            imgBt.frame = CGRectMake(54+i*(w+5+10+61), 23, w+15, 14);
            _imgIV = [ReduceCode creatImageVeiwWithFrame:CGRectMake(w+5, 2, 10, 10) imageName:imgArr[i] isCorner:NO];
            [imgBt addSubview:_imgIV];
            imgBt.tag = 10+i;
            [imgBt addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArray addObject:imgBt];
            [self.imgArray addObject:_imgIV];
            [_headView addSubview:imgBt];
        }
        
        [ToolUtil createLine:48 superView:_headView lift:17 width:_hudView.frame.size.width-34 hight:1 color:[UIColor colorWithRed:245.0/255 green:118.0/255 blue:157.0/255 alpha:1]];
        
        NSArray *fontTitleArr = @[@"逛街",@"吃饭",@"看电影",@"游戏"];
        CGFloat y = [ToolUtil autoWidth:@"逛街" width:0 height:0 num:12];
        for (int j=0; j<4; j++) {
            UIButton *fontBt = [UIButton buttonWithType:UIButtonTypeCustom];
            fontBt.frame = CGRectMake(17+(y+27)*j, 63, y+27, 12);
            [_headView addSubview:fontBt];
            UILabel *fontL = [ReduceCode creatLabelWithFrame:fontBt.bounds title:fontTitleArr[j] titleColor:[ImageUtil colorWithRgb:0xFFFFFF] font:12 textAlignment:NSTextAlignmentCenter];
            [fontBt addSubview:fontL];
        }
        
        CGFloat z = [ToolUtil autoWidth:@"¥" width:0 height:0 num:20];
        UILabel *RMBlb = [ReduceCode creatLabelWithFrame:CGRectMake(30, 148, z, 20) title:@"¥" titleColor:[UIColor colorWithRed:252.0/255 green:90.0/255 blue:114.0/255 alpha:1] font:20 textAlignment:NSTextAlignmentLeft];
        [_headView addSubview:RMBlb];
        
        
        
        contentTF = [[UITextField alloc] initWithFrame:CGRectMake(z+45+10, 143, 142.5, 30)];
        contentTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        contentTF.font = FONT(14);
        contentTF.delegate = self;
        contentTF.layer.borderWidth = 1.0f;
        contentTF.layer.cornerRadius = 3;
        contentTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 30)];
        contentTF.leftViewMode = UITextFieldViewModeAlways;
        contentTF.layer.borderColor = [UIColor colorWithRed:228.0/255 green:228.0/255 blue:228.0/255 alpha:1].CGColor;
        [_headView addSubview:contentTF];
        
        [ToolUtil createLine:_headView.frame.size.height-41 superView:_headView lift:17 width:_headView.frame.size.width-34 hight:1 color:[UIColor colorWithRed:228.0/255 green:228.0/255 blue:228.0/255 alpha:1]];
        [ToolUtil createVerticalLine:_headView.frame.size.height-30 superView:_headView lift:_headView.frame.size.width/2-0.5 height:20 color:[UIColor colorWithRed:228.0/255 green:228.0/255 blue:228.0/255 alpha:1]];
        
        UIButton *sureBt = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBt.frame = CGRectMake(0, _headView.frame.size.height-40, _headView.frame.size.width/2-0.5, 40);
        CGFloat q = [ToolUtil autoWidth:@"确定" width:0 height:0 num:14];
        UILabel *sureL = [ReduceCode creatLabelWithFrame:CGRectMake(54, 13, q, 14) title:@"确定" titleColor:[UIColor colorWithRed:206.0/255 green:126.0/255 blue:141.0/255 alpha:1] font:14 textAlignment:NSTextAlignmentLeft];
        [sureBt addTarget:self action:@selector(sureBtClick:) forControlEvents:UIControlEventTouchUpInside];
        [sureBt addSubview:sureL];
        [_headView addSubview:sureBt];
        
        UIButton *cancelBt = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBt.frame = CGRectMake(_headView.frame.size.width/2+0.5, _headView.frame.size.height-40, _headView.frame.size.width/2-0.5, 40);
        UILabel *cancelL = [ReduceCode creatLabelWithFrame:CGRectMake(_headView.frame.size.width/2-54-q, 13, q, 14) title:@"取消" titleColor:[UIColor colorWithRed:104.0/255 green:104.0/255 blue:104.0/255 alpha:1] font:14 textAlignment:NSTextAlignmentLeft];
        [cancelBt addTarget:self action:@selector(cancelBtClick:) forControlEvents:UIControlEventTouchUpInside];
        [cancelBt addSubview:cancelL];
        [_headView addSubview:cancelBt];
    }
    return _headView;
}
#pragma mark --TextField ---
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSScanner      *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *numbers;
    NSRange         pointRange = [textField.text rangeOfString:@"."];
    if (pointRange.location == NSNotFound && range.location != 0){
        if (range.location >= 4) {
            if ([string isEqualToString:@"."] && range.location == 4) {
                return YES;
            }
            return NO;
        }
    }
//    if (pointRange.location!=NSNotFound && range.location > pointRange.location + 2) {
//        return NO;
//    }
//    if (textField.text.length > 6) {
//        
//        return NO;
//        
//    }
    if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) ){
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        
    }else{
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    }
    if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] ){
        return NO;
    }
    short remain = 1; //保留 number位小数
    NSString *tempStr = [textField.text stringByAppendingString:string];
    NSUInteger strlen = [tempStr length];
    if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
        if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时，如果再输入“.”则被视为无效。
            return NO;
        }
        if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”，当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数，则视当次输入无效。
            return NO;
        }
    }
    NSRange zeroRange = [textField.text rangeOfString:@"0"];
    if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
        if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时，再输入不为“0”或者“.”的字符时，则将此输入替换输入框的这唯一字符。
            textField.text = string;
            return NO;
        }else{
            if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时，并且没有“.”字符时，如果当此输入的字符为“0”，则视当此输入无效。
                if([string isEqualToString:@"0"]){
                    return NO;
                }
            }
        }
    }
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) ){
        return NO;
    }else{
        return YES;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
//- (void)textForNumber:(UITextField *)textfield {
//    
//    if (textfield.text.length>5) {
//        textfield.text = [textfield.text substringToIndex:5];
//        NSString *str = contentTF.text;
//        textfield.text = [NSString stringWithFormat:@"%4.1f",[str floatValue]];
////        textfield.text = [textfield.text substringToIndex:11];
//    }
//}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.hudView endEditing:YES];
}

-(UIView *)hudView
{
    if (!_hudView){
        _hudView = [[UIView alloc] initWithFrame:CGRectMake((WindowWidth-240)/2,64.5+80,240, 240)];
        _hudView.alpha = 1.0f;
        _hudView.layer.borderWidth = 0.6;
        UIImageView *back = [ReduceCode creatImageVeiwWithFrame:_hudView.bounds imageName:@"quoteBG" isCorner:NO];
        [_hudView addSubview:back];
        _hudView.layer.borderColor = [UIColor clearColor].CGColor;
//        _hudView.backgroundColor = [UIColor yellowColor];
        _hudView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
        
        
        
        [self addSubview:self.hudView];
        
//        self.hudView.userInteractionEnabled=YES;
    }
    return _hudView;
}
#pragma mark --UIButton Click---
- (void)Click:(UIButton *)btn {
//    NSLog(@"...");
    for (UIButton *sender in self.btnArray) {
        if (sender.tag != btn.tag) {
            _imgV = self.imgArray[btn.tag-10];
            _imgV.image = [UIImage imageNamed:@"skillLi"];
            NSLog(@"%ld",(long)btn.tag);

            _imgV2 = self.imgArray[sender.tag-10];
            _imgV2.image = [UIImage imageNamed:@"skillNor"];

        }else {

        }
    }
    imgBt = btn;

}
- (void)sureBtClick:(UIButton *)btn {
    
}
- (void)cancelBtClick:(UIButton *)btn {
    [self dismiss];
}
-(UIWindow *)window
{
    if (!_overlayWindow) {
        _overlayWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _overlayWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _overlayWindow.backgroundColor = [UIColor clearColor];
        _overlayWindow.windowLevel = UIWindowLevelAlert;
        _overlayWindow.userInteractionEnabled = YES;
    }
    return _overlayWindow;
}



















@end
